package org.microdream.easy.deploy.service;

import ice.server.comm.BaseOperateResult;
import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import org.apache.commons.collections.CollectionUtils;
import org.microdream.easy.deploy.dao.DAppRepository;
import org.microdream.easy.deploy.po.CachePo;
import org.microdream.easy.deploy.po.DApp;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: app服务层
 * @author:沈明可
 * @create: 2019-09-09 21:08
 */
@Service
public class DAppService extends BaseServiceImpl<DApp, Integer> {

    @Resource
    private DAppRepository dAppRepository;

    @Override
    public CommRepository<DApp, Integer> getRepository() {
        return dAppRepository;
    }

    @Override
    public Integer getIdenty(DApp dApp) {
        return dApp.getId();
    }

    /**
     * 分页查询app信息
     */
    public Map searchByPage(Map gson) {
        Map<String,Object> resultMap = new HashMap();
        Page<DApp> p = this.findPage(gson);
        List<DApp> apps = p.getContent();
        if (!CollectionUtils.isEmpty(apps)) {
            apps.forEach(app -> {
                app.set("showPackType", CachePo.getName("pack_type", app.getPackType()));
            });
        }
        resultMap.put("total", p.getTotalElements());
        resultMap.put("rows", apps);
        resultMap.put("page", p.getNumber());
        return resultMap;
    }


    /**
     * 功能描述:〈app新增〉
     *
     * @param app 实体类
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/10 10:27
     * User :
     */
    public BaseOperateResult add(DApp app) {
        return BaseOperateResult.add(save(app));
    }


    /**
     * 功能描述:〈app修改〉
     *
     * @param app 实体类
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/10 10:27
     * User :
     */
    public BaseOperateResult update(DApp app) {
        return BaseOperateResult.update(save(app));
    }


    /**
     * 功能描述:〈根据id删除〉
     *
     * @param ids 需要删除的app的主键id以,拼接
     * @return : ice.server.comm.BaseOperateResult
     * @author : 沈明可
     * @date : 2019/9/10 10:39
     * User :
     */
    public BaseOperateResult delete(String ids) {
        return BaseOperateResult.delete(this.delete(ParamBulider.build().addIn("id", ids).get()));
    }

    /**
    * 功能描述:〈编码查重 同项目下禁止重复app编码〉
    * @param appCode app编码
     * @param projectCode 项目编码
    * @return : ice.server.comm.BaseOperateResult
    * @author : 沈明可
    * @date : 2019/9/10 10:50
    * User :
    */
    public BaseOperateResult findByAppCodeAndProjectCode(String appCode,String projectCode ){
        boolean flag = false;
        List<DApp> apps = this.find(ParamBulider.build().addEqual("appCode", appCode).addEqual("projectCode", projectCode).get());
        if(org.apache.commons.collections.CollectionUtils.isEmpty(apps)){
           flag =true;
        }else {
            DApp dApp = apps.get(0);
            if(dApp.getAppCode().equals(appCode)){
                flag =true;
            }
        }
        return  flag?BaseOperateResult.success("编码可用"):BaseOperateResult.error("编码重复不可用");
    }

}
